// by Xeno
//#define __DEBUG__
#define THIS_FILE "x_shcfunctions.sqf"
#include "x_setup.sqf"

#ifdef __GROUPDEBUG__
// the group debug functions are not available in a mission release, no need to compile them with compileFinal
FUNC(map_group_count_marker) = {
	scriptName "d_fnc_map_group_count_marker";
	_mname = "all_groups_yeah";
	_mtext = "Groups: %1, alive units: %2, inf without leader: %3";
	[_mname, [3000,100,0],"ICON","ColorBlack",[2,2],format [_mtext, 0,0],0,"mil_dot"] call FUNC(CreateMarkerLocal);
	while {true} do {
		GVAR(all_marker_groups) = GVAR(all_marker_groups) - [objNull, grpNull];
		_grpcounter = count GVAR(all_marker_groups);
		_units = 0;
		_remgrps = [];
		{
			_alu = _x call FUNC(GetAliveUnitsGrp);
			if (_alu > 0) then {
				_units = _units + _alu;
			} else {
				_remgrps set [count _remgrps, _x];
			};
		} forEach GVAR(all_marker_groups);
		if (count _remgrps > 0) then {
			GVAR(all_marker_groups) = GVAR(all_marker_groups) - _remgrps;
		};
		_mname setMarkerTextLocal format [_mtext, _grpcounter,_units, GVAR(infunitswithoutleader)];
		sleep 1;
	};
};

GVAR(gcounter) = -1;
GVAR(infunitswithoutleader) = 0;
FUNC(groupmarker) = {
	scriptName "d_fnc_groupmarker";
	private ["_grp", "_mname", "_mnamel", "_leader", "_p1", "_wps", "_idx", "_curwppos", "_gname"];
	PARAMS_1(_grp);
	waitUntil {sleep 0.221;(_grp call FUNC(GetAliveUnitsGrp)) > 0};
	_helper = str(_grp);
	_gname = if (_helper != "") then {_helper} else {__INC(GVAR(gcounter));str(GVAR(gcounter))};
	_mname = _gname + "dgrp";
	_mnamel = _mname + "lm";
	_mnamewp = _mname + "wpm";
	_gname = _gname;
	sleep 1;
	GVAR(all_marker_groups) set [count GVAR(all_marker_groups), _grp];
	_vec = vehicle leader _grp;
	_mtype = if (_vec == leader _grp) then {
		if (side _grp == blufor) then {"b_inf"} else {"o_inf"}
	} else {
		switch (true) do {
			case (_vec isKindOf "Wheeled_APC"): {if (side _grp == blufor) then {"b_mech_inf"} else {"o_mech_inf"}};
			case (_vec isKindOf "Car"): {if (side _grp == blufor) then {"b_motor_inf"} else {"o_motor_inf"}};
			case (_vec isKindOf "Tank"): {if (side _grp == blufor) then {"b_armor"} else {"o_armor"}};
			case (_vec isKindOf "Helicopter"): {if (side _grp == blufor) then {"b_air"} else {"o_air"}};
			case (_vec isKindOf "Plane"): {if (side _grp == blufor) then {"b_plane"} else {"o_plane"}};
			case (_vec isKindOf "StaticCannon"): {if (side _grp == blufor) then {"b_art"} else {"o_art"}};
			case (_vec isKindOf "StaticMortar"): {if (side _grp == blufor) then {"b_mortar"} else {"o_mortar"}};
			default {if (side _grp == blufor) then {"b_support"} else {"n_support"}};
		}
	};
	[_mname, [0,0,0],"ICON",(if (side _grp == opfor) then {"ColorRed"} else {"ColorBlue"}),[0.8,0.8],_gname,0,_mtype] call FUNC(CreateMarkerLocal);
	_gname = _gname + " (%1)";
	while {true} do {
		if (isNull _grp || {(_grp call FUNC(GetAliveUnitsGrp)) == 0}) exitWith {
			deleteMarkerLocal _mname;
			deleteMarkerLocal _mnamel;
			deleteMarkerLocal _mnamewp;
		};
		_leader = leader _grp;
		if (!isNull _leader) then {
			_p1 = getPosASL _leader;
			_p1 set [2,0];
			_mname setMarkerPosLocal _p1;
			_mname setMarkerTextLocal format [_gname, _grp call FUNC(GetAliveUnitsGrp)];
			_wps = wayPoints _grp;
			_idx = currentWaypoint _grp;
			if (_idx > 0 && {_idx < count _wps}) then {
				_curwppos = waypointPosition (_wps select _idx);
				_curwppos set [2,0];
				_mpos = markerPos _mnamewp;
				if ((_mpos select 0) == 0 &&  {(_mpos select 1) == 0} && {(_mpos select 2) == 0}) then {
					[_mnamewp,_curwppos,"ICON","ColorGreen",[0.7, 0.7],"",0,"waypoint"] call FUNC(CreateMarkerLocal);
				} else {
					_mnamewp setMarkerPosLocal _curwppos;
				};
				[_p1, _curwppos, _mnamel] call FUNC(linemaker2);
			} else {
				deleteMarkerLocal _mnamel;
				deleteMarkerLocal _mnamewp;
			};
		} else {
			deleteMarkerLocal _mnamel;
			deleteMarkerLocal _mnamewp;
		};
		sleep (0.6 + random 0.2);
	};
};
#endif

/*
FUNC(execWPMaker) = {
	__TRACE_1("execWPMaker","_this");
	_ar = _this select 1;
	case (_ar select 0) do {
		case "WPX": {(_ar select 1) call FUNC(MakePatrolWPX)};
		case "WPX2": {(_ar select 1) call FUNC(MakePatrolWPX2)};
		case "DEFEND": {(_ar select 1) spawn FUNC(taskDefend)};
		case "ATTACK": {(_ar select 1) call FUNC(AttackWP)};
		case "TAKEWFWP": {(_ar select 1) call FUNC(TakeWFWP)};
	};
	{
		if (alive _x) then {
			_x disableAI "FSM";
			_x enableAI "FSM";
		};
	} forEach (units ((_ar select 1) select 0));
};
*/

#ifndef __TT__
GVAR(airmarker_counter) = 0;

GVAR(isledefmarker_counter) = 0;
#endif